algorithms and cleanup