code cleanup and small fixes