cleaned up code, wrote out functions