fixed memory leak and improved jobs code