JC: refectoring and fixed bugs. spec passes.