Code optimized and bug fixes