added helper functions and comments