Use API instead of calling external commands

No need to start a full shell just to perform some operations.