added output image option
fixed bug which caused artifacts in high contrast images
cleaned code a bit