Removed a bug AND simplified the code