Cleaned up main() by splitting it into number of helper functions