Fixed the first two functions to make it more efficient. Added comments