Previously the gradient was being calculated before taking the boundaries into account, resulting in the edges being too hard or too soft
Also clean up some stuff
thank you sarbian!