Cleaned up, added types, fixed warnings.