fixes and code reshuffle to better organize file