cleaned up function structure, added some helper functions