bug fixes after optimizing