refactored speed, added better comments for functions