Refactor code; fix problems with line numbers and if-then statements