Added the function prologues and cleaned up the code