Code re-factoring & fixing