Works but need optimization and clean up