Fixed race conditions, cleaned up frame code