Refactor and fix LR/SC implementation (#217)