Added comments and function prologues to code