Added JSON support and fixed bugs