Polish code to make it less hacky.