refactored code to get some vectorization

took down 2 secs on 1024 image.