tidied function order and added comments