Moved some coding to the right place