Re-factoring and minor changes to the python and java versions