Refactored Code and added Gamma correction