Optimize and refactor crop
Merge pull request from int-index/master