Some refactoring and some pixel manipulation algorithms