Cleaned and commented code for final submission