Move object to function to get lexical closure.

Some basic refactoring to not pollute object through unnecessary functions.