Refactor the code to make it more flexible

Add more unit tests
Make it compatible with Java 6