Made objective function easier to read and cleaned code