Major command rewrite and logic fixes