Pure Bash implementation, actually works