Working version, cleaned up code.