moving code to header file and fixing indentation