Cleaned up patches and did some scala porting