Modified, cleaned up, and commented code