Random cleanup. Move file opening operations to main(), rename pid file, and other house keeping