Another massive rewrite
Updated to work for single node