Fix recursive formula, Clean code, add writeJSON function