refactoring most of the code (json still has to be used though)