Tidied up mouse tracking, removed some redundant code and added comments for clarity