Updated with new tests and implementation from other machine