Moved some code to proper places