performance optimizations and removing alpha channel from calculating