Major refactor + fixes