Reformat code, extract some methods
Fix issue 254
Some more private methods extracted