Refactored, added JSON support