Cleanup and fixed #317