Moved some code to Util.java + minor cleanup