moving some more code to the right place and coding style