cleaned up and commented code and functions