Fixed memory errors and added pic rotate logic