Code cleanup and tests adjusted