Fixed scope handling and added more tests