Some attempt at code cleanup