Corrections to code for function below java 1.7, some bugs corrected