fixed error when playback reaches end, some refactoring along the way