layout fixes and improved logic