Code cleanup and ts fix