Major code restructure and fix client-side tests