code cleanup, making direct solution work for the general case