Fixed occurrence and scheduler logic

Also commented and refactored some code