Code cleanup and small fixes