Fixes lib path and improves code logic