Tidied function order and removed extra variables