Fixed build and improved logic