bug fixes. tested up to 2 iterations.