Finished stack code and algorithm for testing