Fixed classloading issues and refactored a bit