Refactored to allow easy CPU implementation