cleaned up code, added helper functions