Add more documentation, refactor code a bit